﻿/*
ChangeTracking (a library for POCO change tracking).

Copyright (C) 2014 Dmitry Merzagitovich Arslanov

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*/

using System.Xaml;

namespace ChangeTrackingSample
{
    public sealed class Document
    {
        private ChangeTracking.Transaction transaction;

        public Database Database { get; private set; }

        public ChangeTracking.ChangeTracker Tracker { get; private set; }

        public string FileName { get; set; }

        public Document()
        {
            Database = new Database();
            Tracker = new ChangeTracking.ChangeTracker(Database);
        }

        public void DetectChanges(string description)
        {
            if (transaction != null)
            {
                transaction.Commit(description);
                transaction.Dispose();
            }
            transaction = Tracker.StartTransaction();
        }

        public void Save(string fileName)
        {
            XamlServices.Save(fileName, Database);
            Tracker.Changed = false;
            FileName = fileName;
        }

        public static Document Load(string fileName)
        {
            Document doc = new Document();
            doc.Database = XamlServices.Load(fileName) as Database;
            return doc;
        }
    }
}
